/*
    Message
    author: zhao xin
    date: 2016.9
*/


! function($) {
    'use strict';

    $(document).ready(function() {
        // create container for message popups
        var $msgContainer = $('<div id="mui-msg-container"></div')
        $msgContainer.appendTo('body');

        // remove dom node when exit animation ends
        $msgContainer.on('transitionend', '.exit', function(e) {
            $(e.target).remove();
        });
    });

    function _msg(type, content, duration) {
        var _duration = duration || 4000,
            _html = '<span class="mui-msg-content">' + content + '</span>';

        switch (type) {
            case 'msg':
                _html = content;
                break;
            case 'success':
                _html = '<i class="fa fa-check-circle icon-success"></i>' + _html;
                break;
            case 'info':
                _html = '<i class="fa fa-info-circle icon-info"></i>' + _html;
                break;
            case 'warning':
                _html = '<i class="fa fa-info-circle icon-warning"></i>' + _html;
                break;
            case 'error':
                _html = '<i class="fa fa-times-circle icon-error"></i>' + _html;
                break;
            default:
                break;
        }

        var $el = $('<div class="mui-msg">' + _html + '</div>');

        $('#mui-msg-container').append($el);

        // auto close
        setTimeout(function() {
            $el.addClass('exit');
        }, _duration);
    }

    window.Message = {
        msg: function(content, duration) {
            _msg('msg', content, duration);
        },

        info: function(content, duration) {
            _msg('info', content, duration);
        },

        success: function(content, duration) {
            _msg('success', content, duration);
        },

        warning: function(content, duration) {
            _msg('warning', content, duration);
        },

        error: function(content, duration) {
            _msg('error', content, duration);
        },

        loading: function(msg) {
            var msg = msg || '加载中';
            var _html = '<div class="mui-loading-wrapper"><div class="mui-loading-msg">'
                + '<i class="fa fa-circle-o-notch fa-spin mui-loading-icon"></i><span class="mui-loading-text">' 
                + msg + '</span></div></div>';

            $('body').append($(_html));
        },

        unLoading: function(container) {
            $('.mui-loading-wrapper').remove();
        }
    }

}(jQuery);